//
//  YLRegularFinancingProgress.m
//  YLFinanceService
//
//  Created by she1323 on 2017/9/13.
//  Copyright © 2017年 she1323. All rights reserved.
//

#import "YLRegularFinancingProgressView.h"
#import "HYCircleProgressView.h"
#import "YLProgressMoneyBaseView.h"
#import <Masonry.h>
#import "YLProgressHeaderModel.h"
@interface YLRegularFinancingProgressView ()
// 进度条
@property (weak, nonatomic) HYCircleProgressView *progressView;
@property (weak, nonatomic)  UIButton *leftButton;
@property (weak, nonatomic)  UIButton *rightButton;
@property (nonatomic, weak) YLProgressMoneyBaseView *base1;
@property (nonatomic, weak) YLProgressMoneyBaseView *base2;

@end

@implementation YLRegularFinancingProgressView

- (instancetype)initWithFrame:(CGRect)frame
{
    if (self = [super initWithFrame:frame]) {
        
        _toLeftSubject = [RACSubject subject];
        _toRightSubject = [RACSubject subject];
        
        [self setUpUI];
    }
    return self;
}

- (void)createSlipButton
{
    __weak typeof(self) weakSelf = self;
    UIButton *leftButton = [self buttonWithImage:@"leftSlip" action:@selector(toLeft)];
    _leftButton = leftButton;
    leftButton.hidden = YES;
    [leftButton mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_equalTo(weakSelf).mas_offset(10);
        make.top.bottom.mas_equalTo(weakSelf);
        make.width.mas_equalTo(20);
    }];
    
    UIButton *rightButton = [self buttonWithImage:@"rightSlip" action:@selector(toRight)];
    _rightButton = rightButton;
    [rightButton mas_makeConstraints:^(MASConstraintMaker *make) {
        make.right.mas_equalTo(weakSelf).mas_offset(-10);
        make.top.bottom.mas_equalTo(weakSelf);
        make.width.mas_equalTo(20);
    }];
    
    
    YLProgressMoneyBaseView *base1 = [YLProgressMoneyBaseView baseView];
    _base1 = base1;
    [self addSubview:base1];
    [base1 mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.mas_equalTo(weakSelf).mas_offset(25);
        make.right.mas_equalTo(rightButton.mas_left).mas_offset(-5);
        make.left.mas_equalTo(weakSelf.mas_centerX);
    }];
    
    YLProgressMoneyBaseView *base2 = [YLProgressMoneyBaseView baseView];
    _base2 = base2;
    [self addSubview:base2];
    [base2 mas_makeConstraints:^(MASConstraintMaker *make) {
        make.bottom.mas_equalTo(weakSelf).mas_offset(-25);
        make.right.mas_equalTo(rightButton.mas_left).mas_offset(-5);
        make.left.mas_equalTo(weakSelf.mas_centerX);
    }];
    
    
}


- (void)toRight
{
    [_toRightSubject sendNext:nil];
}

- (void)toLeft
{
    [_toLeftSubject sendNext:nil];
}

- (UIButton *)buttonWithImage:(NSString *)imageName action:(SEL)action
{
    UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
    button.imageView.contentMode = UIViewContentModeScaleAspectFit;
    [button setImage:[UIImage imageNamed:imageName] forState:UIControlStateNormal];
    [button addTarget:self action:action forControlEvents:UIControlEventTouchUpInside];
    [self addSubview:button];
    return button;
}

+ (instancetype)regularFinancingProgress
{
    return [[NSBundle mainBundle] loadNibNamed:NSStringFromClass([self class]) owner:nil options:nil].lastObject;
}


- (void)setGroup:(YLProgressHeaderGroup *)group
{
    _group = group;
    _base1.model = group.group.firstObject;
    _base2.model = group.group.lastObject;
    _progressView.backgroundStrokeColor = group.backgroundStrokeColor;
    _progressView.progressStrokeColor = group.progressStrokeColor;
     [_progressView setProgress:group.progress animated:YES];
    
}

//- (void)setViewModel:(YLRegularFinancingViewModel *)viewModel
//{
//    _viewModel = viewModel;
//    _base1.model = viewModel.models.firstObject;
//    _base2.model = viewModel.models.lastObject;
//    
//}

- (void)setUpUI
{
    self.backgroundColor = [UIColor whiteColor];
    CGFloat wh = self.frame.size.height - 60;
    HYCircleProgressView *progress = [[HYCircleProgressView alloc] initWithFrame:CGRectMake(60 , 30, wh, wh)];
    progress.progressLineWidth = 7;
    progress.hiddenText = YES;
    progress.backgroundLineWidth = 7;
    progress.backgroundStrokeColor = YLColor(215, 60, 49);
    progress.progressStrokeColor = YLColor(246, 180, 63);
    _progressView = progress;
    [_progressView setProgress:0 animated:YES];
    [self addSubview:progress];
    [self createSlipButton];
}
@end
